package Leetcode.ArrayAndLinkedList.ContainerOfMaxAreaWater11;

/**
 * 该方法为双指针法
 * 左右边界向中间汇合  左右夹逼
 * 用i和j分别指向开头和结尾，比较两个大小，小的那边往内缩
 *
 */

/**
 * 次数2
 */
public class Solution2 {
    public int maxArea(int[] height) {
        int Area = 0;
        int i = 0;
        int j = height.length - 1;
        while(i < j) {
            int maxHeight = height[i] < height[j] ? height[i++] : height[j--];
            int tmp = (j - i +1) * maxHeight;
            Area = Math.max(tmp,Area);
        }

        return Area;
    }

    public static void main(String[] args) {
        int[] height= {1,8,6,2,5,4,8,3,7};
        Solution2 solution = new Solution2();
        System.out.println(solution.maxArea(height));
    }
}







/**
 *  public int maxArea(int[] height) {
 *         int maxArea = 0;
 *         for(int i = 0 , j = height.length-1; i < j ;) {
 *             int maxHeight = height[i] < height[j] ? height[i++]: height[j--];
 *             int area = (j-i+1) * maxHeight;
 *             maxArea = Math.max(area,maxArea);
 *         }
 *         return maxArea;
 *     }
 */
